package com.sxj.corejava.code13_JDBC;

import java.sql.*;

public class Test12_元数据 {

    public static void main(String[] args) {
        User user = new User("李白", "123456", "13612345698", "江苏-苏州");
        insertReturnPrimaryKey(user);
    }

    public static void test01() throws DataAccessException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();

            // 获取数据库的元数据
            DatabaseMetaData dbmd = conn.getMetaData();
            System.out.println("数据库厂商名:" + dbmd.getDatabaseProductName());
            System.out.println("数据库版本号:" + dbmd.getDatabaseProductVersion());
            System.out.println("数据库驱动:" + dbmd.getDriverName());
            System.out.println("数据库驱动版本:" + dbmd.getDriverVersion());
            System.out.println("URL地址:" + dbmd.getURL());
            System.out.println("用户名:" + dbmd.getUserName());

            String sql = new StringBuffer()
                    .append(" select id,username,password,phone,address ")
                    .append(" from t_user ")
                    .toString();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            // 获取结果集元数据
            ResultSetMetaData rsmd = rs.getMetaData();
            int count = rsmd.getColumnCount();
            System.out.println("结果集中共有" + count + "列");
            for (int i = 1; i <= count; i++) {
                System.out.println("第" + i + "列字段的名称:" + rsmd.getColumnName(i));
                System.out.println("第" + i + "列字段的类型:" + rsmd.getColumnTypeName(i));
                System.out.println("第" + i + "列字段所在的表:" + rsmd.getTableName(i));

            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw e;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DataAccessException(e);
        } finally {
            JDBCUtil.close(conn, ps, rs);
        }
    }

    public static Integer insertReturnPrimaryKey(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Integer id = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_user ")
                    .append("   (username,password,phone,address)")
                    .append(" values ")
                    .append("   (?,?,?,?)")
                    .toString();
            // 如果保存后需要返回主键,则在获取状态集的时候需要进行设置
            // 通过参数二来设置是否返回主键
            // 其值是一个常量,有两种选择
            // RETURN_GENERATED_KEYS:返回主键,开发中一般只用返回
            // NO_GENERATED_KEYS:不返回主键
            ps = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getPhone());
            ps.setString(4, user.getAddress());
            // 执行DML操作
            ps.executeUpdate();
            rs = ps.getGeneratedKeys();
            ResultSetMetaData rsmd = rs.getMetaData();
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                // GENERATED_KEY
                System.out.println("第" + i + "列字段的名称:" + rsmd.getColumnName(i));
                System.out.println("第" + i + "列字段的类型:" + rsmd.getColumnTypeName(i));
                System.out.println("第" + i + "列字段所在的表:" + rsmd.getTableName(i));
            }
        } catch (DataAccessException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return id;
    }

}
